package com.pomr.controller.equipmentstorage;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pomr.model.equipmentstorage.EquipmentInStorage;
import com.pomr.model.synthesize.Equipment;
import com.pomr.model.synthesize.Manufacturer;
import com.pomr.model.sys.Agency;
import com.pomr.model.sys.User;
import com.pomr.service.equipmentstorage.EquipmentStorageService;
import com.pomr.service.synthesize.ManufacturerService;
import com.pomr.service.sys.AgencyService;
import com.pomr.service.sys.UserService;
import com.pomr.util.common.JWT;
import com.pomr.util.common.ResponseData;
import com.pomr.util.page.Page;
import com.pomr.util.page.PageParam;

/**
 * 设备入库
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/equipmentStorage")
public class EquipmentStorageController {
	@Autowired
	@Qualifier("equipmentStorageServiceImpl")
	private EquipmentStorageService equipmentStorageService;
	@Autowired
	@Qualifier("agencyServiceImpl")
	private AgencyService agencyService;
	@Autowired
	@Qualifier("equipmentServiceImpl")
	private com.pomr.service.synthesize.EquipmentService equipmentService;
	@Autowired
	@Qualifier("userServiceImpl")
	private UserService userService;
	@Autowired
	@Qualifier("manufacturerServiceImpl")
	private ManufacturerService manufacturerService;
	
	/**
	 * 入库前查询信息（设备、厂商-通过选择机构关联重新查询、用户-通过选择机构关联重新查询、机构）
	 * @param request
	 * @return
	 */
	@RequestMapping("inter-addbefore")
	@ResponseBody
	public ResponseData addStorageBefore(HttpServletRequest request){
		ResponseData pd = null;
		try {
			User user = JWT.findUserByToken(request);
			pd = ResponseData.ok();
			//获取所有设备
			List<Equipment> equipments = equipmentService.searchAllEquipment();
			if(equipments == null || equipments.size()<=0){
				pd = ResponseData.custom("未找到可以入库的设备");
			}else{
				pd.putDataValue("equipments", equipments);
				//获取所有的机构包含下属机构
				List<Agency> agencys = agencyService.searchAgencysOfUser(user);
				pd.putDataValue("agencys", agencys);
				//获取当前机构的人员
				List<User> users = userService.searchUsersOfAgency(user);
				pd.putDataValue("users", users);
				//获取厂商信息
				List<Manufacturer> manufacturers = manufacturerService.searchManufacturersOfAgency(user);
				pd.putDataValue("manufacturers", manufacturers);
			}
		} catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	/**
	 * 选择机构后，触发查询厂商、人员信息
	 * @param request
	 * @return
	 */
	@RequestMapping("inter-addselect")
	@ResponseBody
	public ResponseData searchInfosOfSelect(@RequestBody Agency agency){
		ResponseData pd = null;
		try {
			pd = ResponseData.ok();
			//获取当前机构的人员
			List<User> users = userService.searchUsersByAgency(agency);
			pd.putDataValue("users", users);
			//获取厂商信息
			List<Manufacturer> manufacturers = manufacturerService.searchManufacturersByAgency(agency);
			pd.putDataValue("manufacturers", manufacturers);
		} catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	/**
	 * 设备入库
	 * @param manufacturer
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/inter-add",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData EquipmentInStorage(@RequestBody com.pomr.model.equipmentstorage.EquipmentInStorage instor,HttpServletRequest request){
		ResponseData pd = null;
		try{
			User user = JWT.findUserByToken(request);
			instor.setCreateUser(user);
			instor.setCreateTime(new Date());
//			instor.setAgency(user.getAgency());
			//TODO 判断数量与sn编码数是否相符
			if(equipmentStorageService.inStorage(instor)){
				pd = ResponseData.ok();
			}else{
				pd = ResponseData.fail();
			}
		}catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	/**
	 * 查询入库信息  （厂商、机构、设备、入库日期）
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/inter-search",method=RequestMethod.GET)
	@ResponseBody
	public Page<List<EquipmentInStorage>> searchEquipmentInStorages(PageParam param){
		try{
			String page = param.getPage();
			String limit = param.getLimit();
			Map<String, String> params = param.getParams();
			String equipmentid = params.get("equipmentid");
			String agencyid = params.get("agencyid");
			String manufacturerid = params.get("manufacturerid");
			String xsjg = params.get("status");
			String time1 = params.get("time1");
			String time2 = params.get("time2");
			return equipmentStorageService.searchEquipmentInStorages(equipmentid, agencyid, manufacturerid, xsjg,time1, time2, page, limit);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 设备入库记录详细信息
	 * @param manufacturer
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/inter-searchone",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData searchEquipmentInStorage(@RequestBody com.pomr.model.equipmentstorage.EquipmentInStorage instor){
		ResponseData pd = null;
		try{
			instor = equipmentStorageService.searchEquipmentInStorageById(instor);
			if(instor!=null){
				pd = ResponseData.ok();
				pd.putDataValue("storage", instor);
			}else{
				pd = ResponseData.notFound();
			}
		}catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}

	/**
	 * 删除入库记录--如果库中信息已使用 不能删除
	 * @param map id:入库记录主键  del：（1更新库存记录，0：）
	 * @return
	 */
	@RequestMapping(value="/inter-delete",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData deleteEquipmentInStorage(@RequestBody Map<String,String> map) {
		ResponseData pd = null;
		try {
			String ids = map.get("id");
			String del = map.get("del");
			if(equipmentStorageService.deleteEquipmentInStorageById(ids.split(","),del)){
				pd = ResponseData.ok();
			}else{
				pd = ResponseData.fail();
			}
		} catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	/**
	 * 审核记录，计入库存
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/inter-review",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData reviewEquipmentInStorage(@RequestBody Map<String,String> map) throws Exception{
		ResponseData pd = null;
		String ids = map.get("id");
		if(equipmentStorageService.reviewEquipmentInStorageById(ids.split(","))){
			pd = ResponseData.ok();
		}else{
			pd = ResponseData.custom("审核记录失败");
		}
		return pd;
	}
}
